<?php
namespace App\Router;

use App\Di\Di;

class FrameRouter implements Router
{
    /**
     * 配置实例
     * @var
     */
    private $config;
    /**
     * 请求对象实例
     * @var
     */
    private $request;
    /**
     * 默认模块
     * @var string
     */
    private $moduleName = '';
    /**
     * 控制器名称
     * @var string
     */
    private $controllerName = '';
    /**
     * 默认操作
     * @var string
     */
    private $actionName = '';
    /**
     * 类文件
     * @var string
     */
    private $classPath = '';
    /**
     * 请求uri
     * @var string
     */
    private $requestUri = '';
    /**
     * 路由策略映射
     * @var string
     */
    private $routeStrategy = '';
    /**
     * @var \App\Di\Di
     */
    private $di;

    /**
     * 魔术方法_get
     *
     * @param string $name 属性名称
     *
     * @return mixed
     */
    public function __get ($name = '')
    {
        return $this->$name;
    }

    /**
     * 魔术方法_set
     *
     * @param string $name  属性名称
     * @param string $value 属性值
     */
    public function __set ($name = '', $value = '')
    {
        $this->$name = $value;
    }

    /**
     * 路由初始化
     *
     * @param Di $di
     *
     * @throws \Exception
     */
    public function initialize (Di $di)
    {
        // 注入当前对象到容器中
        $di->setShared('router', $this);

        //request uri
        $this->request    = $di->get('request');
        $this->requestUri = $this->request->server('REQUEST_URI');

        $this->moduleName     = '';
        $this->controllerName = '';
        $this->actionName     = '';
        $this->strategyJudge();
        $this->start();
    }

    /**
     * 路由策略决策 路由规则
     *
     */
    public function strategyJudge ()
    {
        if (!empty($this->routeStrategy))
        {
            return;
        }
        #code.....
    }

    /**
     * 路由机制 路由启动
     *
     * @throws \Exception
     */
    public function start ()
    {
        //判断模块在不在
        if (!in_array(strtolower($this->moduleName), []))
        {
            throw new \Exception('Module:' . $this->moduleName . 'NOT FOUND');
        }
        //判断控制器存不存在
        if (!class_exists($this->classPath))
        {
            throw new \Exception('Controller:' . $this->controllerName . 'NOT FOUND');
        }
        $controller = new $this->classPath();
        if (!method_exists($controller, $this->actionName))
        {
            throw new \Exception('Method:' . $this->controllerName . 'NOT FOUND');
        }

        //调用操作
        $actionName = $this->actionName;
        //获取返回值
        #code....
    }

}